<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/4/28
 * Time: 14:41
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Order extends Base
{
    /**
     * 全部订单列表
     * @return mixed|\think\response\Json
     */
    public function allOrder(){
        if(request()->isAjax ()){
            $user_username = trim(input('user_username'), ' ');
            $user_phone = trim(input('user_phone'), ' ');
            $order_nickname = trim(input('order_nickname'), ' ');
            $order_phone = trim(input('order_phone'), ' ');
            $order_cate = input('order_cate');
            $order_type = input('order_type');
            $order_status = input('order_status');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $match_cate = input('match_cate');
            
            $where = [];
    
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['order_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['order_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['order_time'] = ['<', $end_time];
            }
            if(isset($user_username) && $user_username != ''){
                $where['user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($order_nickname) && $order_nickname != ''){
                $where['order_nickname'] = ['like', '%' . $order_nickname . '%'];
            }
            if(isset($order_phone) && $order_phone != ''){
                $where['order_phone'] = ['like', '%' . $order_phone . '%'];
            }
            if(isset($order_cate) && $order_cate != ''){
                $where['order_cate'] = $order_cate;
            }
            if(isset($order_type) && $order_type != ''){
                $where['order_type'] = $order_type;
            }
            if(isset($order_status) && $order_status != ''){
                $where['order_status'] = $order_status;
            }
            if(isset($match_cate) && $match_cate != ''){
                $where['match_cate'] = $match_cate;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('order')
                ->join('up_match', 'order_match_id = match_id', 'left')
                ->join('up_u_user', 'order_user_id = user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('order')
                ->join('up_match', 'order_match_id = match_id', 'left')
                ->join('up_u_user', 'order_user_id = user_id', 'left')
                ->field('order_id, order_num, order_price, order_status, order_time, order_pay_time, 
                order_type, order_cate, order_nickname, user_username, user_phone, order_phone, match_cate, 
                match_title')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('order_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('order/allorder');
    }
    
    /**
     * 购买照片订单
     * @return mixed|\think\response\Json
     */
    public function imagesOrder(){
        if(request()->isAjax ()){
            $user_username = trim(input('user_username'), ' ');
            $user_phone = trim(input('user_phone'), ' ');
            $order_num = trim(input('order_num'), ' ');
            $order_type = input('order_type');
            $order_status = input('order_status');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $match_cate = input('match_cate');
            
            $where['order_cate'] = ['IN', '1,2'];
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['order_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['order_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['order_time'] = ['<', $end_time];
            }
            if(isset($user_username) && $user_username != ''){
                $where['user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($order_num) && $order_num != ''){
                $where['order_num'] = ['like', '%' . $order_num . '%'];
            }
            if(isset($order_type) && $order_type != ''){
                $where['order_type'] = $order_type;
            }
            if(isset($order_status) && $order_status != ''){
                $where['order_status'] = $order_status;
            }
            if(isset($match_cate) && $match_cate != ''){
                $where['match_cate'] = $match_cate;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('order')
                ->join('up_match', 'order_match_id = match_id', 'left')
                ->join('up_u_user', 'order_user_id = user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('order')
                ->join('up_match', 'order_match_id = match_id', 'left')
                ->join('up_u_user', 'order_user_id = user_id', 'left')
                ->field('order_id, order_num, order_price, order_status, order_time, order_pay_time, 
                order_type, order_cate, order_nickname, user_username, user_phone, match_title, 
                match_cate')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('order_time desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('order/imagesorder');
    }
    
    /**
     * 购买的赛事照片
     * @return mixed
     */
    public function payImages(){
        $order_id = input('order_id');
        $img_ids = Db::table('up_order')->where('order_id', $order_id)->value('order_image_id');
        $images = Db::table('up_match_images')
            ->where('mi_id', 'IN', $img_ids)
            ->column('mi_url');
        $images = array_filter($images);
        return $this->fetch('order/payimages', ['images' => $images]);
    }
    
    /**
     * 赛事报名订单
     * @return mixed|\think\response\Json
     */
    public function matchOrder(){
        if(request()->isAjax ()){
            $user_username = trim(input('user_username'), ' ');
            $user_phone = trim(input('user_phone'), ' ');
            $order_num = trim(input('order_num'), ' ');
            $order_type = input('order_type');
            $order_status = input('order_status');
            $start_time = input('start_time');
            $end_time = input('end_time');
        
            $where['order_cate'] = ['IN', '3,4'];
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['order_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['order_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['order_time'] = ['<', $end_time];
            }
            if(isset($user_username) && $user_username != ''){
                $where['user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($order_num) && $order_num != ''){
                $where['order_num'] = ['like', '%' . $order_num . '%'];
            }
            if(isset($order_type) && $order_type != ''){
                $where['order_type'] = $order_type;
            }
            if(isset($order_status) && $order_status != ''){
                $where['order_status'] = $order_status;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('order')
                ->join('up_u_user', 'order_user_id = user_id', 'left')
                ->join('up_match', 'match_id = order_match_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('order')
                ->join('up_u_user', 'order_user_id = user_id', 'left')
                ->join('up_match', 'match_id = order_match_id', 'left')
                ->field('order_id, order_num, order_price, order_status, order_time, order_pay_time, 
                order_type, order_cate, order_nickname, user_username, user_phone, match_id, match_title')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('order_time desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('order/matchorder');
    }
}